import pymysql
from Mapper.wav_mapper import WavMapper, WavFile

"""WAV文件的业务实现类"""
class WavImpl:
    def __init__(self, db_config):
        self.db_config = db_config
        self.connection = None
        self.cursor = None

    """使用上下文管理器管理数据库连接"""
    def __enter__(self):
        self.connection = pymysql.connect(**self.db_config)
        self.cursor = self.connection.cursor()
        return self

    """关闭数据库连接"""
    def __exit__(self, exc_type, exc_val, exc_tb):
        if self.cursor:
            self.cursor.close()
        if self.connection:
            self.connection.commit()
            self.connection.close()

    """插入WAV文件记录"""
    def insert_wav(self, wav_file):
        mapper = WavMapper(self.cursor)
        return mapper.insert(wav_file)

    """根据用户英文名查询WAV文件记录"""
    def get_wav_by_user_english_name(self, user_english_name):
        mapper = WavMapper(self.cursor)
        return mapper.select_by_user_english_name(user_english_name)

    """根据用户英文名获取身份信息"""
    def get_user_identity_by_user_english_name(self, user_english_name):
        mapper = WavMapper(self.cursor)
        return mapper.get_user_identity_by_user_english_name(user_english_name)

    """检查用户英文名是否已存在"""
    def check_user_english_name_exists(self, user_english_name):
        mapper = WavMapper(self.cursor)
        return mapper.check_user_english_name_exists(user_english_name)

    """根据用户英文名删除WAV文件记录"""
    def delete_wav_by_user_english_name(self, user_english_name):
        mapper = WavMapper(self.cursor)
        return mapper.delete_by_user_english_name(user_english_name)
